{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 迭代器"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1, 2, 3, 4, \n",
      "1, 2, 3, 4, \n"
     ]
    }
   ],
   "source": [
    "a = [1,2,3,4]\n",
    "it = iter(a) #创建迭代器对象\n",
    "for x in it:\n",
    "    print(x,end=', ')\n",
    "print()    \n",
    "    \n",
    "it = iter(a)    \n",
    "while True:\n",
    "    try:\n",
    "        print (next(it), end=', ')\n",
    "    except StopIteration:\n",
    "        print()\n",
    "        break"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "自定义迭代器"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "abcdefghijklmnopqrstuvwxyz"
     ]
    }
   ],
   "source": [
    "class MyIter:\n",
    "    def __iter__(self):\n",
    "        self.data = 'a'\n",
    "        return self\n",
    "        \n",
    "    def __next__(self):\n",
    "        if self.data <= 'z':\n",
    "            x = self.data\n",
    "            self.data = chr(ord(x)+1)\n",
    "            return x\n",
    "        else:\n",
    "            raise StopIteration\n",
    "        \n",
    "b = MyIter()\n",
    "my_iter = iter(b)\n",
    "for it2 in my_iter:\n",
    "    print(it2,end='')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 生成器"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "1\n",
      "2\n",
      "3\n",
      "5\n",
      "8\n",
      "13\n",
      "21\n",
      "34\n",
      "55\n"
     ]
    }
   ],
   "source": [
    "def fibonacci(n):\n",
    "    a, b, counter = 0, 1, 0\n",
    "    while True:\n",
    "        if counter > n:\n",
    "            return\n",
    "        yield a #生成\n",
    "        a, b = b, a+b\n",
    "        counter += 1\n",
    "        \n",
    "f = fibonacci(10)\n",
    "while True:\n",
    "    try:\n",
    "        print(next(f))\n",
    "    except StopIteration:\n",
    "        break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
